﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LibVLCSharp.Shared;
using MediaPlayer = LibVLCSharp.Shared.MediaPlayer;

namespace WpfLibVLCPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private LibVLC _libVLC;

        private MediaPlayer _player;

        public MainWindow()
        {
            InitializeComponent();
            Core.Initialize();
            _libVLC = new LibVLC();
            _player = new MediaPlayer(_libVLC);
            _player.TimeChanged += Player_TimeChanged;
            videoView.MediaPlayer = _player;

            SpeedCBox.Items.Add(0.5F);
            SpeedCBox.Items.Add(1F);
            SpeedCBox.Items.Add(1.5F);
            SpeedCBox.Items.Add(2F);
        }

        private void Player_TimeChanged(object? sender, MediaPlayerTimeChangedEventArgs e)
        {
            Dispatcher.Invoke(
                new Action(() =>
                {
                    if (!_isDrag)
                    {
                        PlaySlider.Maximum = _player.Length;
                        PlaySlider.Value = _player.Time;
                        TimeText.Text =
                            TimeSpan.FromMilliseconds(_player.Time).ToString(@"hh\:mm\:ss")
                            + "/"
                            + TimeSpan.FromMilliseconds(_player.Length).ToString(@"hh\:mm\:ss"); //总长
                    }
                })
            );
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FileStream fileStream = new FileStream("F:\\Test\\test.mp4", FileMode.Open);
            Media media = new Media(_libVLC, new StreamMediaInput(fileStream));
            _player.Media = media;
            _player.Play();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_player.IsPlaying)
            {
                _player.Pause();
            }
            else
            {
                _player.Play();
            }
        }

        bool _isDrag = false;

        private void PlaySlider_DragStarted(
            object sender,
            System.Windows.Controls.Primitives.DragStartedEventArgs e
        )
        {
            _isDrag = true;
            _player.SetPause(true);
        }

        private void PlaySlider_DragDelta(
            object sender,
            System.Windows.Controls.Primitives.DragDeltaEventArgs e
        )
        {
            _player.Time = Convert.ToInt64(PlaySlider.Value);
        }

        private void PlaySlider_DragCompleted(
            object sender,
            System.Windows.Controls.Primitives.DragCompletedEventArgs e
        )
        {
            _isDrag = false;
            _player.Play();
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_player != null)
            {
                var slider = (Slider)sender;
                _player.Volume = Convert.ToInt32(slider.Value);
            }
        }

        private void SpeedCBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_player != null)
            {
                _player.SetRate((float)SpeedCBox.SelectedItem);
            }
        }
    }
}
